/**
 * 
 */
package com.infosys.cabplanning.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import com.infosys.cabplanning.common.Distance;
import com.infosys.cabplanning.common.Duration;
import com.infosys.cabplanning.common.Leg;
import com.infosys.cabplanning.common.Route;
import com.infosys.cabplanning.common.Route_;
import com.infosys.cabplanning.common.domain.CabDto;
import com.infosys.cabplanning.common.domain.Driver;
import com.infosys.cabplanning.common.domain.SelectTrip;

@Component
public class JsonFormatter {
	
	/**
	 * 
	 * @param objectDTO
	 * @return
	 */
	public String convertToJSON(Object objectDTO){
		return null;
		
	}
	
	/**
	 * 
	 * @param jObject
	 * @return
	 * "status" : "OK"
	 */
	public Route parse(JSONObject jObject){
	    JSONArray jRoutes = null;
	    JSONArray jLegs = null;
	    JSONObject distance = null;
	    JSONObject duration = null;
	    JsonFormatter jsonFormatter = new JsonFormatter();
	    Route routeDTO = new Route();
	    Route_ route_DTO = null;
	    List<Route_> route_s = new ArrayList<Route_>();
	    Leg legDTO = null;
	    List<Leg> legs = new ArrayList<Leg> ();
	    Distance distanceDTO = null; 
	    Duration durationDTO = null;
	    try {           
	        jRoutes = jObject.getJSONArray("routes");
	        /** Traversing all routes */
	        for(int i=0;i<jRoutes.length();i++){   
	        	route_DTO = new Route_();
	            jLegs = ( (JSONObject)jRoutes.opt(i)).getJSONArray("legs");
	            for(int j=0;j<jLegs.length();j++){
	            	legDTO = new Leg();
	            	distance = ( (JSONObject)jLegs.opt(i)).getJSONObject("distance");
	            	duration = ( (JSONObject)jLegs.opt(i)).getJSONObject("duration");
	            	if(null != distance){
	            		distanceDTO  = (Distance)jsonFormatter.convertToObject(distance, new Distance());
	            		System.out.println(distanceDTO.toString());
	            	}
	            	if(null != duration){
	            		durationDTO = (Duration)jsonFormatter.convertToObject(duration, new Duration());
	            		System.out.println(durationDTO.toString());
	            	}
	            	legDTO.setDistance(distanceDTO);
	            	legDTO.setDuration(durationDTO);
	            	legs.add(legDTO);
	            }
	            route_DTO.setLegs(legs);
	            route_s.add(route_DTO);
	        }
	        routeDTO.setRoutes(route_s);
	    } catch (JSONException e) {         
	        e.printStackTrace();
	    }catch (Exception e){  
	    	e.printStackTrace();
	    }
	    return routeDTO;
	} 
	
	/**
	 * 
	 * @param json
	 * @return
	 */
	public Object convertToObject(JSONObject json, Object o) {
		ObjectMapper mapper = new ObjectMapper();
		Distance distance = null;
		Duration duration = null;
		Driver driver = null;
		CabDto cab = null;
		SelectTrip selectTrip = null;
		try {
			if (o instanceof Distance){
				return distance = mapper.readValue(json.toString(), Distance.class);
			} else if(o instanceof Duration){
				return duration = mapper.readValue(json.toString(), Duration.class);
			}else if(o instanceof Driver){
				return driver = mapper.readValue(json.toString(), Driver.class);
			}else if(o instanceof CabDto){
				return cab = mapper.readValue(json.toString(), CabDto.class);
			}
			else if(o instanceof SelectTrip){
				return selectTrip = mapper.readValue(json.toString(), SelectTrip.class);
			}
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(distance);
		System.out.println(duration);
		System.out.println(driver);
		return null;
	}
	
	/**
	 * 
	 * @param json
	 * @param o
	 * @return
	 */
	public List<Object> convertToList(String json,Object o){
		JSONArray jsonMainArr = new JSONArray(json);
		List<Object> list = new ArrayList<Object> ();
		Driver driver = null;
		CabDto cabDto = null;
		SelectTrip selectTrip = null;
		for (int i = 0; i < jsonMainArr.length(); i++) {
			 driver = null;
		     JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
		     if (o instanceof Driver){
		    	 driver = (Driver) convertToObject(childJSONObject, new Driver());
		    	 list.add(driver);
		     }
		     else if(o instanceof CabDto){
		    	 cabDto = (CabDto) convertToObject(childJSONObject, new CabDto());
		    	 list.add(cabDto);
				}
				else if(o instanceof SelectTrip){
					selectTrip = (SelectTrip) convertToObject(childJSONObject, new SelectTrip());
			    	 list.add(selectTrip);
				}
		}
		return list;
	}
}
